{include file="public/layout" /}
<body class="laytp-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form layui-row layui-col-space16 layui-search-admin">
		<div class="layui-inline">
			<label class="layui-form-label">用户ID</label>
			<div class="layui-input-inline">
				<input type="text" name="recordmid" autocomplete="off" class="layui-input" value="{$Request.param.mid}">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">昵称</label>
			<div class="layui-input-inline">
				<input type="text" name="nickname" autocomplete="off" class="layui-input" value="">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-inline">
				<input type="text" name="remark" autocomplete="off" class="layui-input" value="">
			</div>
		</div>

		<div class="layui-btn-container layui-col-xs12">
			<button class="layui-btn" lay-submit lay-filter="demo-table-search">搜索</button>
			<button type="reset" class="layui-btn layui-btn-primary">清空</button>
		</div>
	</form>
	</div>
</div>
<div class="layui-card">
	<div class="layui-card-body">
		<table id="test" lay-filter="test"></table>
	</div>
</div>

<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-sm layuiadmin-btn-list" onclick="setst(0,0)">改为未使用</button>
		<button class="layui-btn layui-btn-sm layuiadmin-btn-list" onclick="setst(0,1)">改为已使用</button>
		<button class="layui-btn layui-btn-sm layuiadmin-btn-list" onclick="datadel(0)">删除</button>
		<button class="layui-btn layui-btn-sm layuiadmin-btn-list" onclick="location.href='{:url('recordexcel')}/couponid/{$Request.param.id}'+urlEncode(datawhere)">导出数据</button>
	</div>
</script>

<script>
	var tableIns
	var datawhere = {};
	var totalnum = 0;
	layui.use(function(){
		var table = layui.table;
		var dropdown = layui.dropdown;
		var laydate = layui.laydate;
		var form = layui.form;

		// 创建渲染实例
		tableIns = table.render({
			elem: '#test',
			url:'{:url("record")}', // 此处为静态模拟数据，实际使用时需换成真实接口
			toolbar: '#toolbarDemo',
			defaultToolbar: ['filter'],
			height: 'full-200', // 最大高度减去其他容器已占有的高度差
			css: [ // 重设当前表格样式
				'.layui-table-tool-temp{padding-right: 145px;}'
			].join(''),
			cellMinWidth: 80,
			totalRow: true, // 开启合计行
			page: true,
			pagebar: '#test-pagebar', // 分页栏模板
			cols: [[
				{type:"checkbox"},
				{field: 'id', title: 'ID',sort:true, width:80},
				{field: 'couponname', title: '优惠券名称'},
				{field: 'nickname', title: '领取人',templet:'<div><img src="{{d.head_pic}}" style="width:40px"> {{d.nickname}}(ID:{{d.user_id}})</div>', width:200},
				{field: 'createtime', title: '领取时间',templet:function(d){ return date('Y-m-d H:i',d.createtime)}},
				{field: 'endtime', title: '到期时间',templet:function(d){ return date('Y-m-d H:i',d.endtime)}},


				{field: 'usetime', title: '使用时间',templet:function(d){ if(d.usetime && d.usetime!=0) return date('Y-m-d H:i',d.usetime);else return ''}},
				{field: 'status', title: '状态',width:150,templet:function(d){
						if(d.status==0){
							if(d.endtime < {:time()}){
								return '<span style="color:#555">已过期</span>';
							}else{
								return '<span style="color:#888">未使用</span>';
							}
						}
						if(d.status==1) return '<span style="color:green">已使用</span>';
					}},
				{field: 'remark', title: '备注'},
				{field: 'operation', title: '操作',templet:function(d){
						var html = '';
						if(d.status==0){
							html +='<button class="layui-btn layui-btn-xs" onclick="setst('+d.id+',1)">改为已使用</button>';
						}
						if(d.status==1){
							html +='<button class="layui-btn layui-btn-xs" onclick="setst('+d.id+',0)">改为未使用</button>';
						}
						html +='<button class="layui-btn layui-btn-xs" onclick="datadel('+d.id+')">删除</button>';
						return html;
					},width:250}
			]],
			done: function(res, curr, count){
				totalnum = count;
				console.log(totalnum)
			},
			error: function(res, msg){
				console.log(res, msg)
			}
		});

		laydate.render({
			elem: '.demo-table-search-date'
		});
		laydate.render({
			elem: '#laydate-rangeLinked',
			type: 'datetime',
			range: ['#table-search-date-start', '#table-search-date-end'],
			rangeLinked: true // 开启日期范围选择时的区间联动标注模式 ---  2.8+ 新增
		});
		// 搜索提交
		form.on('submit(demo-table-search)', function(data){
			var field = data.field; // 获得表单字段
			datawhere = field;
			// 执行搜索重载
			table.reload('test', {
				page: {
					curr: 1 // 重新从第 1 页开始
				},
				where: field // 搜索的字段
			});
			//layer.msg('搜索成功<br>此处为静态模拟数据，实际使用时换成真实接口即可');
			return false; // 阻止默认 form 跳转
		});
	});

	//删除
	function datadel(id){
		var ids = [];
		if(id==0){
			var checkStatus = layui.table.checkStatus('test')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要删除吗？删除后无法恢复！',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('recorddel')}",{ids:ids},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	//改状态
	function setst(id,st){
		var ids = [];
		if(id==0){
			var checkStatus = layui.table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要'+(st==0?'改为未使用':'改为已使用')+'吗?',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('recordsetst')}",{op:'setst',ids:ids,st:st},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
</script>
</body>
</html>